package cn.itcast.order.web;

import cn.itcast.common.config.ComoonConfiguration;
import cn.itcast.common.util.TestUtil;
import cn.itcast.common.redis.CacheService;
import cn.itcast.order.config.OrderConfiguration;
import cn.itcast.order.config.PatternProperties;
import cn.itcast.order.pojo.Order;
import cn.itcast.order.pojo.User;
import cn.itcast.order.service.OrderService;
import cn.itcast.order.service.UserClient;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.TimeUnit;


@RestController
@RefreshScope
@Slf4j
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;//http调远程接口，配置在common公共模块里面，地址:cn.itcast.common.config.BeanConfig
    @Autowired
    private OrderService orderService;
    //获取配置文件
    @Value("${pattern.name}")
    private String name;
    //获取配置文件
    @Autowired
    private PatternProperties patternProperties;
    //获取配置文件
    @Autowired
    private OrderConfiguration orderConfiguration;
    //获取配置文件
    @Autowired
    private ComoonConfiguration comoonConfiguration;
    //feign调接口
    @Autowired
    private UserClient userClient;
    @Autowired
    private CacheService redis; //redis，来自comoon模块

    @RequestMapping("{orderId}")
    public Order queryOrderByUserId(@PathVariable("orderId") Long orderId) {
        System.out.println("进入订单模块查询订单：");
        System.out.println("你好");

        // 根据id查询订单并返回
        return orderService.queryOrderById(orderId);
    }

    //测试公公共模块引入,注意GetMapping第一个/没有
    @RequestMapping("test/{orderId}")
    public String queryTest() {
        System.out.println("获取到编号：" + TestUtil.id);//测试获取公共模块内容

        return TestUtil.id;
    }

    //调远程接口
    @RequestMapping("restTemplate/{orderId}")
    public Order restTemplate(@PathVariable("orderId") Long orderId) {
        System.out.println("调远程接口获取到编号：" + TestUtil.id);
        // 1.查询订单
        Order order = orderService.queryOrderById(orderId);
        // 2.查询用户，直接链接调接口，开启负载均衡后用直链调会出错，所以这个方法尽量少用，关闭负载均衡才可以尝试这个
        //怎么关闭负载均衡，把restTemplate的注解@LoadBalanced删掉
        User user = restTemplate.getForObject("http://localhost:8081/user/" + order.getUserId(), User.class);
        // 3. 将用户信息封装进订单
        order.setUser(user);
        // 4.返回
        return order;
    }

    //调远程接口
    @RequestMapping("restTemplate2/{orderId}")
    public Order restTemplate2(@PathVariable("orderId") Long orderId) {
        System.out.println("调远程接口获取到编号：" + TestUtil.id);
        // 1.查询订单
        Order order = orderService.queryOrderById(orderId);
        // 2.查询用户，这里改为用eureka注册中心服务名调接口，结合负载均衡算法，restTemplate需要加ribbon配置，pom.xml也需要加ribbon配置
        User user = restTemplate.getForObject("http://userservice/user/" + order.getUserId(), User.class);
        // 3. 将用户信息封装进订单
        order.setUser(user);
        // 4.返回
        return order;
    }

    //feign代替restTemplate调远程接口
    @RequestMapping("feignTest/{orderId}")
    public Order feignTest(@PathVariable("orderId") Long orderId) {
        System.out.println("调远程接口获取到编号：" + TestUtil.id);

        // 1.查询订单
        Order order = orderService.queryOrderById(orderId);
        //2021/8/20 使用feign远程调用
        User user = userClient.findById(order.getUserId());
        // 3. 将用户信息封装进订单
        order.setUser(user);
        // 4.返回
        return order;
    }

    //获取配置文件，，热更新
    @RequestMapping("getProperties/{orderId}")
    public String getProperties() {
        log.info("进入获取配置接口");
        System.out.println("获取到编号：" + TestUtil.id);//测试获取公共模块内容
        System.out.println("获取到远程nacos配置：" + patternProperties.getDateformat());
        System.out.println("获取到远程nacos配置：" + name);
        //发现获取不到comoon的配置文件，这里要注意这个bug
        System.out.println("获取comoon本地配置：" + comoonConfiguration.getMyName()+comoonConfiguration.getMyAge()+comoonConfiguration.getMySchool());
        System.out.println("获取order本地配置：" + orderConfiguration.getMyName()+orderConfiguration.getMyAge()+orderConfiguration.getMySchool());
        log.info("获取order本地配置：" + orderConfiguration.getMyName()+orderConfiguration.getMyAge()+orderConfiguration.getMySchool());
        return TestUtil.id+"成功";
    }


    //redis测试
    @RequestMapping("redis/{orderId}")
    public Order redisTest(@PathVariable("orderId") Long orderId) {
        System.out.println("Redis获取到编号：" + TestUtil.id);
        //优先从redis查数据
        Order order =redis.getObject("order",Order.class);
        System.out.println("redis获取到对象：" + JSON.toJSONString(order));
        if(order==null){
            // 1.查询订单
            order = orderService.queryOrderById(orderId);
            //存入redis
            redis.add("order", order,3, TimeUnit.SECONDS);
        }

        // 4.返回
        return order;
    }
}
